<?php

namespace app\api\controller;


use app\api\biz\RoleBiz;
use app\common\controller\ApiController;
use app\common\Result;
use joncc\attribute\attr\AuthNeedless;
use joncc\attribute\attr\Comment;
use joncc\attribute\attr\Params;
use joncc\attribute\attr\PermissionDefine;
use joncc\attribute\attr\PermissionNeedless;
use think\response\Json;


#[Comment('角色')]
#[AuthNeedless]
#[PermissionNeedless]
#[PermissionDefine('list', '列表')]
#[PermissionDefine('add', '新增')]
#[PermissionDefine('edit', '编辑')]
#[PermissionDefine('del', '删除')]
class Role extends ApiController
{

    #[Params('name', Params::STRING, false, "名称")]
    #[Params('field', Params::STRING, false, "查询字段','分隔")]
    #[Params('sort', Params::STRING, false, '排序字段 默认id')]
    #[Params('order', Params::BOOL, false, '倒序')]
    #[Params('page', Params::INT, false, '分页 默认1')]
    #[Params('page_size', Params::INT, false, '分页大小 默认15')]
    public function list(): Json
    {
        $roleBiz = RoleBiz::list(p());
        return Result::success($roleBiz::getData());
    }

    #[Params('name', Params::STRING, true, "名称")]
    #[Params('memo', Params::STRING, false, "备注")]
    public function add(): Json
    {
        $roleBiz = RoleBiz::add(p());
        if ($roleBiz::hasError()) {
            return Result::fail(...$roleBiz::getError());
        }
        return Result::success($roleBiz::getData());
    }

    #[Params('id', Params::INT, true, "ID")]
    #[Params('name', Params::STRING, true, "名称")]
    #[Params('memo', Params::STRING, false, "备注")]
    public function edit(): Json
    {
        $roleBiz = RoleBiz::edit(p());
        if ($roleBiz::hasError()) {
            return Result::fail(...$roleBiz::getError());
        }
        return Result::success($roleBiz::getData());
    }

    #[Params('ids', Params::STRING, true, "ids多个','分隔")]
    public function del(): Json
    {
        $roleBiz = RoleBiz::del(p());
        if ($roleBiz::hasError()) {
            return Result::fail(...$roleBiz::getError());
        }
        return Result::success($roleBiz::getData());
    }
}
